import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import request from '../../../utils/request'
Vue.use(Vuex)


export default new Vuex.Store({
  state: state,
  getters: {
    getLogo(state) {
      return state.showLogo
    },
    getTabs(state) {
      return state.showTabs
    },
    getHeadImg(state) {
      return state.headImg
    },
    getCompanyName(state) {
      return state.companyName
    },
    getLight(state) {
      return state.showlight
    },
    getTreeName(state) {
      return state.treeName
    },
    getToDoCount(state) {
      return state.toDoCount
    },
    getBreadList(state) {
      return state.breadList
    },
    getActiveMenuIndex(state) {
      return state.activeMenuIndex;
    },
  },
  mutations: {
    // 获取全局消息
    setBreadcrumb(state, params) {
      state.breadcrumbList = params;
    },
    getMsg(state) {
      request.axiosPublic.$post('/operation/messageCenter/list', { pageNum: 1, pageSize: 1, beRead: false }).then(res => {
        if (res.code === 200) {
          state.message = Object.assign({ total: res.total }, res.rows[0] || {})
        }
      })
    },
    changeLogin(state, user) {
      state.authorization = user.authorization
      sessionStorage.setItem('authorization_Ygd', user.authorization)
    },
    telephone(state, phone) {
      state.mobile = phone
      sessionStorage.setItem('mobile', phone)
    },
    //
    updateHeadImg(state, imgUrl) {
      state.headUrl = imgUrl
      sessionStorage.setItem('headUrl', imgUrl)
    },
    update(state, obj) {
      let { name, val } = obj;
      state[name] = val;
    },
    //菜单权限
    setMenuPermission(state, obj) {
      state.menuPermission = obj
      // console.log('state.menuPermission', state.menuPermission);
    },
    setYgdUserInfo(state, obj) {
      state.userInfo = obj
      state.userName = obj.username
      sessionStorage.setItem('userName', obj.username)
    },
    clearUserInfo(state, obj) {
      state.userInfo = {}
    },
    //用户信息
    userOrgyg(state, obj) {
      state.userOrgyg = obj
    },
    userOrgNameyg(state, obj) {
      state.userOrgNameyg = obj
    },
    //左上角logo是否显示
    setlogo(state, obj) {
      state.showLogo = obj
    },
    //设置主题颜色
    setlight(state, obj) {
      state.showlight = obj
    },
    //标签页是否显示
    setTabs(state, obj) {
      state.showTabs = obj
    },
    setRuleObj(state, obj) {
      state.ruleObj = { ...obj }
    },
    clearRuleObj(state) {
      state.ruleObj = {}
    },
    setPermissionSet(state, obj) {
      state.permissionSet = [...obj]
    },
    setActiveMenuIndex(state, activeMenuIndex) {
      state.activeMenuIndex = activeMenuIndex
    },
    clearActiveMenuIndex(state) {
      state.activeMenuIndex = ''
    },
    clearBreadList(state) {
      state.breadList = ''
    },
    setBreadList(state, str) {
      state.breadList = str

    },
    setActiveMenu(state, data) {
      console.log(data)
      state.activeMenu = data
    },
    setIsThirdMenu(state, str) {
      state.isThirdMenu = str
    },
    setThirdMenuArr(state, str) {
      state.thirdMenuArr = str
    },
    // 菜单的url存储
    pushBreadListPath(state, path) {
      state.breadList = path
      // if (!state.breadList) {
      //   state.breadList = path
      // } 
      // else {
      //   if (state.breadList.indexOf(path) === -1) {
      //     state.breadList += `,${path}`
      //   }
      // }

      // state.breadList?state.breadList+=`,${path}`:state.breadList=path
    },
    clearTagsView(state) {
      state.tagsView = [];
    },
    addVisitedView(state, view) {
      if (state.tagsView.some(v => v.path === view.path)) return
      state.tagsView.push(
        Object.assign({}, view, {
          title: view.meta.title || 'no-name'
        })
      )
    },
    delVisitedView(state, view) {
      for (const [i, v] of state.tagsView.entries()) {
        if (v.path === view.path) {
          state.tagsView.splice(i, 1)
        }
      }
    },
    //
    //设置头像
    setHeadImg(state, img) {
      state.headImg = img
    },
    //设置公司名称
    setCompanyName(state, companyName) {
      state.companyName = companyName
    },
    //
    SET_ButtonPermission: (state, buttonPermission) => {
      state.permission = buttonPermission
    },
    UPDATE_VISITED_VIEW: (state, view) => {
      for (const v of state.tagsView) {
        if (v.path === view.path) {
          v.fullPath = view.fullPath
          break
        }
      }
    },
    setWorkflowNodeType: (state, typeList) => {
      state.WorkflowNodeType = typeList
    },
    setTreeName: (state, str) => {
      state.treeName = str
    },
    setToDoCount: (state, num) => {
      state.toDoCount = num
    }
  },
  actions: {
    getInfo({ commit }, that) {
      return new Promise((resolve, reject) => {
        that
          .axios('/user/getUser')
          .then(response => {
            const { data } = response
            if (!data) {
              reject(data.msg)
            }
            const { buttonPermission, username, permissionSet } = data
            if (!permissionSet || !permissionSet[0]) {
              reject('用户没有页面菜单权限,无法访问系统')
            }
            commit('SET_ButtonPermission', buttonPermission)
            // commit('SET_ROLES', permissionSet)
            commit('SET_NAME', username)
            resolve(data)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    delView({ commit, state }, view) {
      return new Promise(resolve => {
        commit('delVisitedView', view)
        resolve({
          visitedViews: [...state.tagsView]
        })
      })
    }
  }
})
